<?php

namespace App\Http\Controllers\Api\Third;

use \App\Http\Controllers\Controller;
use DB;

//公共
class CommonApiController extends Controller
{
    // // private $url='http://22.40.212.237:8899/rmcp-gqj-web/api/';//接口对接地址
    // protected $url='';//接口对接地址
    // protected $orgId='';//试验中心组织机构id
    // protected $thirdMap=null;
    // protected $thirdId=null;
    // protected $blgSgsId=null; //试验中心所属市公司单位id
    // protected function __construct($third){
    //     $this->thirdId = $third->id;
    //     $this->url = $third->base_url;
    //     $orgId = DB::table('setting')->where('id',1)->value('third_orgId');
    //     if(!$orgId){
    //         return '试验中心组织机构id获取失败';
    //     }
    //     $this->orgId = $orgId;
    //     $this->blgSgsId = $third->blgSgsId;
    //      //dd($this->orgId);
    // }
    protected function getStdToolItems($std_id=0,$tool_id=0){
        $data = DB::table('a_std_tool_item_rel as a')
                ->leftJoin('a_testitem as b','a.testitem_id','=','b.id')
                ->where(['a.ishide'=>0])
                ->select('b.id as item_id','b.name as item_name','a.std_id','a.tool_id')
                ->orderBy('a.sort','asc')
                ->get();
        $data2 = [];
        foreach ($data as $item) {
            $data2[$item->std_id.'-'.$item->tool_id][] = $item;
        }
        if ($std_id && $tool_id) {
            return $data2[$std_id.'-'.$tool_id];
        }
        // dd($data2);
        return $data2;
    }

    //判断样品是否可以添加到 a_third_data 表中
    protected function isInsert($thirdId,$third_id2){
        $info = DB::table('a_third_data')->where(['third_id'=>$thirdId,'third_id2'=>$third_id2])->orderBy('id','desc')->first();
        if($info){
            if(!$info->bar_code){
                return 0;
            }
            $exist = DB::table('a_sample_info as b')
                ->leftJoin('a_contract as c','b.contract_id','=','c.id')
                ->where('b.bar_code',$info->bar_code)
                ->where('c.status','<',20)
                ->exists();
            return $exist ? 0 : 1;
        }
        return 1;
    }
}
